<template>
  <div class="departments-container">
    <!-- app-container提供padding -->
    <div class="app-container">

      <el-card v-loading="loading" class="tree-card">

        <tree-tools
          :node-data="company"
          :is-root="true"
          @add-depts="handleAddDepts"
        />

        <!-- 内容：树形 -->
        <el-tree default-expand-all :data="departs" :props="defaultProps">
          <!-- node是el-tree偏底层一点的节点信息 -->
          <!-- data是我们提供的数据层面的信息 -->
          <template #default="{ data }">
            <!-- tree的节点的宽度，默认由内容撑开 -->
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>

      <add-dept
        ref="addDept"
        :node-data="nodeData"
        :dept-list="deptList"
        :show-dialog.sync="showDialog"
        @add-depts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { reqGetDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'

export default {
  name: 'Departments',
  // label 标签内容文字
  // children 子节点内容
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      // 树形的数据
      departs: [],
      // 列表式的数据
      deptList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      showDialog: false,
      nodeData: {}, // 正在操作的部门,
      loading: false

    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 发送 请求，获取数据，渲染结构
    async getDepartments() {
      const { data: { depts, companyName }} = await reqGetDepartments()
      this.company.name = companyName
      this.company.id = '' // 特殊处理，纯粹是为了后面校验等处理

      // 将列表式的数据也存一份，便于查找
      this.deptList = depts
      // 处理成树形结构
      const treeData = transListToTreeData(depts, '')
      this.departs = treeData
    },

    handleAddDepts(nodeData) {
      this.nodeDate = nodeData
      this.showDialog = true
      this.$nextTick(() => {

      })
      // 显示，发请求获取员工简单列表 (父组件想要调用子组件的方法) ref $refs
      // this.$refs.addDept.getSimpleUserList()
    },
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$refs.addDept.getSimpleUserList()
      this.$nextTick(() => {
        this.$refs.addDept.getDepartDetail()
      })
    }
  }
  // 显示控制制之后，利用$refs操作时，千万要注意$nextTick
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
